
class Hook {
  constructor(ages) {
    this.ages = ages
    this.taps = []
    this._x = []
  }

  tap(options, fn) {
    this._tap('sync', options, fn)
  }

  compiler(options) {
    throw new Error(`${options.type}未找到合适的编译器`)
  }

  call(...ages) {
    this.cell = this._createCall('sync')

    return this.cell(...ages)
  }

  _createCall(type) {
    return this.compiler({
      ages: this.ages,
      taps: this.taps,
      type
    })
  }

  _tap(type, options, fn) {
    if (typeof options === 'string') {
      options = { name: options }
    } else if (typeof options !== 'object' || options === null) {
      throw new Error('options 必须是对象或则字符串')
    }

    options = Object.assign({ type, fn }, options)

    const i = this.taps.length

    this.taps[i] = options
  }
}

module.exports = Hook